﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 创建项目结构文件夹
/// 只添加不会删除文件夹
/// </summary>
public class EditorInsertDirectory
{
    [MenuItem("项目管理/0.创建资源目录结构")]
    public static void InsertDir()
    {
        Debug.Log("InsertDir");

        InsertDirCfg insertDirCfg = AssetDatabase.LoadAssetAtPath<InsertDirCfg>(
            "Assets/InsertDirectoryStructure/InsertDirCfg.asset");
        if (insertDirCfg != null)
        {
            Debug.Log("Load InsertDirCfg Succeed");
        }
        else
        {
            Debug.LogWarning("Load InsertDirCfg Failed");
            insertDirCfg = InsertDirCfg.CreateInstance<InsertDirCfg>();
        }

        EditorUtility.DisplayProgressBar("创建项目结构文件夹", "", 0f);
        TryCreateDir(insertDirCfg.DirList, Application.dataPath);
        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
        Debug.Log("InsertDir Succeed");
    }

    static void TryCreateDir(List<LinkNode> nodes, string currPath)
    {
        if (nodes != null && nodes.Count > 0)
        {
            foreach (LinkNode node in nodes)
            {
                string dir = currPath + "/" + node.FolderName;
                EditorUtility.DisplayProgressBar("创建项目结构文件夹", "正在创建文件夹：" + dir, 0f);
                if (!Directory.Exists(dir))
                {
                    try
                    {
                        Directory.CreateDirectory(dir);
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogError(e);
                    }
                }

                TryCreateDir(node.NextList, dir);
            }
        }
    }
}
